package com.ruoyi.zcxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zcxt.domain.ZcClassify;
import com.ruoyi.zcxt.service.IZcClassifyService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 资产分类（树）Controller
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@RestController
@RequestMapping("/zcxt/classify")
public class ZcClassifyController extends BaseController
{
    @Autowired
    private IZcClassifyService zcClassifyService;

    /**
     * 查询资产分类（树）列表
     */
    @PreAuthorize("@ss.hasPermi('zcxt:classify:list')")
    @GetMapping("/list")
    public AjaxResult list(ZcClassify zcClassify)
    {
        List<ZcClassify> list = zcClassifyService.selectZcClassifyList(zcClassify);
        return success(list);
    }

    /**
     * 导出资产分类（树）列表
     */
    @PreAuthorize("@ss.hasPermi('zcxt:classify:export')")
    @Log(title = "资产分类（树）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcClassify zcClassify)
    {
        List<ZcClassify> list = zcClassifyService.selectZcClassifyList(zcClassify);
        ExcelUtil<ZcClassify> util = new ExcelUtil<ZcClassify>(ZcClassify.class);
        util.exportExcel(response, list, "资产分类（树）数据");
    }

    /**
     * 获取资产分类（树）详细信息
     */
    @PreAuthorize("@ss.hasPermi('zcxt:classify:query')")
    @GetMapping(value = "/{cfId}")
    public AjaxResult getInfo(@PathVariable("cfId") Long cfId)
    {
        return success(zcClassifyService.selectZcClassifyByCfId(cfId));
    }

    /**
     * 新增资产分类（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:classify:add')")
    @Log(title = "资产分类（树）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcClassify zcClassify)
    {
        zcClassify.setCreateBy(getUsername());
        return toAjax(zcClassifyService.insertZcClassify(zcClassify));
    }

    /**
     * 修改资产分类（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:classify:edit')")
    @Log(title = "资产分类（树）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcClassify zcClassify)
    {
        Long cfId = zcClassify.getCfId();
        if (zcClassify.getParentId().equals(cfId)){
            return error("修改分类'" + zcClassify.getCfName() + "'失败，上级分类不能是自己");
        }
        else if (StringUtils.equals(ZcxtConstants.CLASSIFY_DISABLE,zcClassify.getStatus()) && zcClassifyService.selectNormalChildrenCfById(cfId) > 0){
            return error("该分类包含未停用的子部门！");
        }
        zcClassify.setUpdateBy(getUsername());
        return toAjax(zcClassifyService.updateZcClassify(zcClassify));
    }

    /**
     * 删除资产分类（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:classify:remove')")
    @Log(title = "资产分类（树）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cfIds}")
    public AjaxResult remove(@PathVariable Long[] cfIds)
    {
        return toAjax(zcClassifyService.deleteZcClassifyByCfIds(cfIds));
    }
}
